package com.vitabuybuy.tv.dialog

import android.content.Context
import android.view.View
import android.view.animation.Animation
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ItemAddress1Binding
import com.vitabuybuy.tv.databinding.PopupBiddingBinding
import com.vitabuybuy.tv.dto.AddressListDto
import com.library.common.base.BindRecycAdapter
import com.library.common.toast.XToast
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AnimationHelper
import razerdp.util.animation.TranslationConfig

/**
 * 竞标出价弹出框
 */
class BiddingPopup(
    context: Context,
    securityDeposit:Double,
    addresslist: MutableList<AddressListDto>,
    private var onConfirm: (address: AddressListDto) -> Unit,
    private var onAgreement: () -> Unit
) : BasePopupWindow(context) {

    private lateinit var addressListAdapter: AddressListAdapter
    private lateinit var mBinding: PopupBiddingBinding

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_bidding)
    }

    init {
        addressListAdapter.setNewData(addresslist)
        mBinding.tvEarnestMoney.text = String.format(context.getString(R.string.price_with_value,securityDeposit))
    }

        override fun onViewCreated(contentView: View) {
            super.onViewCreated(contentView)
            mBinding = DataBindingUtil.bind<ViewDataBinding>(contentView) as PopupBiddingBinding

        setViewClickListener(View.OnClickListener { dismiss() }, mBinding.ivClose)
        mBinding.tvEarnestMoney.text =
            String.format(context.getString(R.string.price_with_value), 100f)
        mBinding.tvBidRequestAgreement.text =
            String.format(context.getString(R.string.bid_request_agreement), "XXXX")

        setViewClickListener(View.OnClickListener {
            if(!mBinding.tvBidRequestAgreement.isChecked){
                XToast.normal(context.getString(R.string.pls_read_and_agree_agreement))
                return@OnClickListener
            }
            for(bean in addressListAdapter.mData){
                if(bean.isDefaulted){
                    onConfirm.invoke(bean)
                    dismiss()
                    return@OnClickListener
                }
            }
        }, mBinding.btnConfirm)

        addressListAdapter = AddressListAdapter()
        mBinding.recyclerView.apply {
            adapter = addressListAdapter
            layoutManager = LinearLayoutManager(context)
        }
        addressListAdapter.setOnItemClickListener { _, item, _ ->
            for(bean in addressListAdapter.mData){
                bean.isDefaulted = false
            }
            item.isDefaulted = !item.isDefaulted
            addressListAdapter.notifyDataSetChanged()
        }
        mBinding.tvViewAgreement.setOnClickListener {
            onAgreement.invoke()
        }
    }

    override fun onCreateShowAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withTranslation(TranslationConfig.FROM_BOTTOM)
            .toShow()
    }

    override fun onCreateDismissAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withTranslation(TranslationConfig.TO_BOTTOM)
            .toDismiss()
    }

    class AddressListAdapter : BindRecycAdapter<ItemAddress1Binding, AddressListDto>() {
        override fun onBindViewHolder(
            mBinding: ItemAddress1Binding,
            itemData: AddressListDto,
            holder: RecyclerView.ViewHolder,
            position: Int
        ) {
            mBinding.itemData = itemData
        }
        override fun normalViewId(viewType: Int): Int {
            return R.layout.item_address_1
        }
    }
}